<template>
  <section class="live-integral-container">
    <el-collapse-transition>
      <section v-show="searchshow" class="main-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input v-model.trim="listQuery.userRealname" placeholder="输入用户名称搜索" class="search-input" @keyup.enter.native="handleFilter" />
        <section class="search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button type="primary" @click="handleReset">重置</el-button>
        </section>
      </section>
    </el-collapse-transition>

    <section class="main-buttons">
      <section class="main-buttons-title">
        <span>直播积分</span>
      </section>
      <section class="main-buttons-btns">
        <el-button style="margin-left: 10px" @click="$router.back()">返回</el-button>
        <el-button style="margin-left: 10px" @click="handleSynchronous">同步用户信息</el-button>
        <el-button v-if="active==='3'" style="margin-left: 10px" @click="handleMultiSet">全部下发积分</el-button>
      </section>
    </section>
    <section class="main-tab">
      <el-tabs v-model="active" @tab-click="handleTabClick">
        <el-tab-pane v-for="item in types" :key="item.id" :label="item.name" :name="item.id" />
      </el-tabs>
      <p v-if="showMsg" style="color: red;font-size: 14px;margin:0;"><i class="el-icon-info"></i>该直播为第三方链接，记录为进入直播间的用户</p>
    </section>
    <section class="main-table">
      <v-table :list="list" :tag="key" :list-loading="listLoading" :filter-column="filtercolumn" :selection="true" @selection-change="handleSelectionChange">
        <el-table-column label="操作" fixed="right" align="center" min-width="120" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button v-if="scope.row.isGetIntegral===0" type="text" size="mini" @click="handleIssued(scope.row)">下发积分</el-button>
          </template>
        </el-table-column>
      </v-table>
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </section>
  </section>
</template>
<script>
import Pagination from '@/components/Pagination'
import VTable from '@/components/Table'
import { queryPageIntegral, Synchronization, issued } from '../api'
import CryptoJS from '@/utils/aesJiaJM'
export default {
  name: 'LiveIntegral',
  components: { Pagination, VTable },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 10,
        userRealname: ''
      },
      searchshow: true,
      search: {
        userRealname: '',
        source: '',
        briefIntroduction: ''
      },
      list: [],
      total: 0,
      listLoading: false,
      selectList: [],
      types: [{
        id: '1',
        name: '全部'
      }, {
        id: '2',
        name: '已发'
      }, {
        id: '3',
        name: '满足条件未发'
      }, {
        id: '4',
        name: '未满足条件'
      }],
      active: '1',
      key: 'LiveIntegral',
      showMsg:false

    }
  },
  created() {
    if(this.$route.query.t==='LIVETYPE_LINK'){
      this.showMsg=true
    }
    this.getList()
  },
  methods: {
    /**
     * 批量积分下发
     */
    handleMultiSet() {
      this.$confirm('确认全部下发积分吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        issued({ liveActivityId: this.$route.query.i }).then(res => {
          this.$message({ type: 'success', message: '操作成功' })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },

    /**
     * 积分下发
     */
    handleIssued(row) {
      this.$confirm('确认下发积分?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        issued({ liveActivityId: this.$route.query.i, userId: row.userId }).then(res => {
          this.$message({ type: 'success', message: res.data.data })
          this.getList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },

    filtercolumn(scope, item) {
      if (item === 'userIdnumber' || item === 'phoneNumber') {
        return CryptoJS.$aesJieM(scope.row[item])
      } else if (item === 'isGetIntegral') {
        return scope.row[item] === 1 ? '是' : '否'
      } else {
        return scope.row[item]
      }
    },

    handleReset() {
      this.listQuery.page = 1
      this.listQuery.userRealname = ''
      for (const i in this.search) {
        if (this.search['' + i + ''] instanceof Array) {
          this.search['' + i + ''] = []
        } else {
          this.search['' + i + ''] = ''
        }
      }
      this.getList()
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        liveActivityId: this.$route.query.i
      }
      if (this.searchshow) {
        query['userRealname'] = this.listQuery.userRealname
      } else {
        query['userRealname'] = this.search.userRealname
        query['source'] = this.search.source
        query['briefIntroduction'] = this.search.briefIntroduction
      }
      if (this.active === '2') {
        query.isGetIntegral = '1'
      } else if (this.active === '3') {
        query.type = '1'
        query.isGetIntegral = '0'
      } else if (this.active === '4') {
        query.type = '0'
      }
      return new Promise((resolve, reject) => {
        queryPageIntegral(query).then(response => {
          this.list = response.data.data.rows
          this.total = response.data.data.total
          this.listLoading = false
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    },
    handleSelectionChange(arr) {
      this.selectList = arr
    },
    handleTabClick() {
      this.getList()
    },
    handleSynchronous() {
      const loading = this.$loading({
        lock: true,
        text: '请稍后',
        spinner: 'el-icon-loading'
      })
      Synchronization({ liveActivityId: this.$route.query.i }).then(res => {
        loading.close()
        this.$message.success(res.data.data)
        this.getList()
      }).catch(error => { loading.close(error) })
    }
  }
}
</script>
<style lang="scss">
@import '@/styles/mixin.scss';

.live-integral-container {
  padding: 10px;
  .main-search {
    background-color: #fff;
    padding: 0 10px;
    .svg-container {
      padding: 0 5px 0 10px;
      color: #bfced6;
      vertical-align: middle;
      display: inline-block;
    }
    .search-input {
      width: 60%;
      position: relative;
      &:after {
        content: '';
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }
      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;
        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }
    .search-btn {
      display: inline-block;
      float: right;
      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }
      .search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .search-inner {
        padding-top: 10px;
        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }
          .el-input {
            position: relative;
            &.el-date-editor input {
              padding-left: 30px;
            }
            .el-input__inner {
              border: none;
              padding: 0;
            }
            &:after {
              content: '';
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }
      .search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }
  .classify-tab {
    width: 100%;
    margin: 10px 0;
    padding: 0 10px;
    background-color: #fff;
    display: flex;
    border-radius: 4px;
    .el-tabs__nav-wrap::after {
      display: none;
    }
    .el-tabs__header {
      margin: 0;
    }
  }
  .main-buttons {
    display: flex;
    line-height: 36px;
    margin-top: 10px;
    .main-buttons-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      font-weight: bold;
      color: rgba(51, 51, 51, 1);
    }
  }
}
</style>
